﻿using System;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.Common
{
    /// <summary>
    /// 数据访问类ResourceStatus。
    /// </summary>
    public class ResourceStatus
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ResourceStatus()
        { }
        #region  成员方法
        /// <summary>
        /// 获取资源状态
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="RESOURCEID">资源ID</param>
        /// <returns></returns>
        public Model.Common.ResourceStatus GetModel(string SUBSYSTEMID, string RESOURCETYPEID, string RESOURCEID)
        {

            StringBuilder strSql = new StringBuilder();
            //有需要将该SQL语句优化修改成视图
            strSql.Append("select SUBSYSTEMID,RESOURCETYPEID,RESOURCETYPENAME,RESOURCETABLE ");
            strSql.Append(" from RESOURCETYPE ");
            strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and RESOURCETYPEID=:RESOURCETYPEID ");
            OracleParameter[] parameters = {
                    new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
                                           new OracleParameter(":RESOURCETYPEID", OracleType.VarChar,25)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = RESOURCETYPEID;

            Model.Common.ResourceStatus model = new Model.Common.ResourceStatus();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.RESOURCETYPEID = ds.Tables[0].Rows[0]["RESOURCETYPEID"].ToString();
                model.RESOURCETYPENAME = ds.Tables[0].Rows[0]["RESOURCETYPENAME"].ToString();
                model.SUBSYSTEMID = ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
                strSql.Remove(0, strSql.Length);//清除strSql
                strSql.Append("select RESOURCEID,RESOURCENAME,RESOURCESTATUS ");
                strSql.Append(" from  ");
                strSql.Append(ds.Tables[0].Rows[0]["RESOURCETABLE"].ToString());
                strSql.Append(" where RESOURCEID=:RESOURCEID ");
                OracleParameter[] ps = { new OracleParameter(":RESOURCEID", OracleType.VarChar, 25) };
                ps[0].Value = RESOURCEID;

                DataSet ds1 = OracleHelper.Query(strSql.ToString(), ps);
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    model.RESOURCEID = ds1.Tables[0].Rows[0]["RESOURCEID"].ToString();
                    model.RESOURCENAME = ds1.Tables[0].Rows[0]["RESOURCENAME"].ToString();
                    model.RESOURCESTATUS = ds1.Tables[0].Rows[0]["RESOURCESTATUS"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取资源状态数据集
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="TYPE">资源类型</param>
        /// <returns></returns>
        public DataSet GetList(string SUBSYSTEMID, string RESOURCETYPEID, string TYPE)
        {

            StringBuilder strSql = new StringBuilder();
            //有需要将该SQL语句优化修改成视图
            strSql.Append("select RT.SUBSYSTEMID,RT.RESOURCETYPEID,RT.RESOURCETYPENAME,RB.RESOURCEID,RB.RESOURCENAME,CMAS.ENTITYSTATUS AS RESOURCESTATUS ");
            strSql.Append(" from RESOURCETYPE RT,RESOURCEBASE RB,COM_MGR_ADP_STATUS CMAS");
            strSql.Append(" where RT.SUBSYSTEMID=:SUBSYSTEMID and RT.RESOURCETYPEID=:RESOURCETYPEID and RT.TYPE=:TYPE and RT.RESOURCETYPEID=RB.RESOURCETYPEID and ");
            strSql.Append(" RB.RESOURCENAME=CMAS.ENTITYNAME and RB.RESOURCETYPEID=CMAS.ENTITYTYPE ");
            OracleParameter[] parameters = {
                    new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
                                           new OracleParameter(":RESOURCETYPEID", OracleType.VarChar,25),
                                           new OracleParameter(":TYPE", OracleType.VarChar,20)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = RESOURCETYPEID;
            parameters[2].Value = TYPE;
            return OracleHelper.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RT.SUBSYSTEMID,RT.RESOURCETYPEID,RT.RESOURCETYPENAME,RB.RESOURCEID,RB.RESOURCENAME,CMAS.ENTITYSTATUS AS RESOURCESTATUS ");
            strSql.Append(" from RESOURCETYPE RT,RESOURCEBASE RB,COM_MGR_ADP_STATUS CMAS ");
            strSql.Append(" where RT.RESOURCETYPEID=RB.RESOURCETYPEID and  RB.RESOURCENAME=CMAS.ENTITYNAME  and RB.RESOURCETYPEID=CMAS.ENTITYTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by RT.SUBSYSTEMID,RT.RESOURCETYPEID,RB.RESOURCEID");
            return OracleHelper.Query(strSql.ToString());
        }
        #endregion  成员方法

        #region 获取设备属性字段
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RT.TYPEID,RT.TYPENAME,RT.TYPECNNAME,CRI.QUERYFROMVIEW ,CRF.ENFIELD,CRF.CHFIELD,CRF.DATATYPE,CRF.TRANSTABLE,CRF.TRANSCOL,CRF.TRANSEDCOL ");
            strSql.Append(" from RESOURCETYPE RT,CFG_RESOURCE_INFO CRI,CFG_RESOURCE_FIELDS CRF ");
            strSql.Append(" where CRF.TABLENAME=CRI.QUERYFROMVIEW and RT.TABLENAME=CRI.RESID and RT.TYPEID=:TYPEID order by SERIAL");
            OracleParameter[] parameters = {
                    new OracleParameter(":TYPEID", OracleType.VarChar,25)};
            parameters[0].Value = TYPEID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null) {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TABLENAME">资源对应的表明</param>
        /// <param name="ISVISIBLE">字段是否显示(为空显示所有,N查询不显示,Y查询显示字段)</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TABLENAME, string ISVISIBLE)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RP.TYPEID,RP.TYPENAME,RP.TYPECNNAME,CRI.QUERYFROMVIEW ,CRF.ENFIELD,CRF.CHFIELD,CRF.DATATYPE,CRF.TRANSTABLE,CRF.TRANSCOL,CRF.TRANSEDCOL ");
            strSql.Append(" from V_RESOURCE_PER RP,CFG_RESOURCE_INFO CRI,CFG_RESOURCE_FIELDS CRF ");
            strSql.Append(" where ");
            if (ISVISIBLE.Length > 0)
            {
                strSql.Append("  CRF.ISVISIBLE='" + ISVISIBLE + "' and ");
            }
            strSql.Append("  CRF.TABLENAME=CRI.MODIFYINFOTABLE and RP.TABLENAME=CRI.RESID and RP.TABLENAME=:TABLENAME  ");
            strSql.Append("  order by TO_NUMBER(SERIAL)");
            OracleParameter[] parameters = {
                    new OracleParameter(":TABLENAME", OracleType.VarChar,25)};
            parameters[0].Value = TABLENAME;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取资源页签
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetResourceTab(string TYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select TYPEID,NETWORKID,TYPENAME,TYPECNNAME,TABLENAME,RTYPE from V_RESOURCE_PER ");
            strSql.Append(" where TYPEID=:TYPEID order by RTYPE desc");
            OracleParameter[] parameters = {
                    new OracleParameter(":TYPEID", OracleType.VarChar,25)};
            parameters[0].Value = TYPEID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取资源值(属性或性能)
        /// </summary>
        /// <param name="TABLENAME">查询资源表名</param>
        /// <param name="RESOURCETYPEID">查询资源ID(如果资源类型ID没有，就直接使用表明，如果资源类型ID存在就查询相应的视图)</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetResourceValue(string TABLENAME,string RESOURCETYPEID, string strWhere)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_tablename", OracleType.VarChar,100),
                    new OracleParameter("p_resourcetypeid", OracleType.VarChar,100),
					new OracleParameter("p_strwhere", OracleType.VarChar,400),
                    new OracleParameter("v_cursor", OracleType.Cursor)};
            parameters[0].Value = TABLENAME;
            parameters[1].Value = RESOURCETYPEID;
            parameters[2].Value = strWhere;

            parameters[0].Direction = ParameterDirection.Input;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[3].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GET_PER_VALUE", parameters, "table1");
        }
         /// <summary>
        /// 获取机房的所有资源
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public DataSet GetRoomEquip(string ROOMID)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_roomid", OracleType.VarChar,25),
                    new OracleParameter("p_cursor", OracleType.Cursor)};
            parameters[0].Value = ROOMID;

            parameters[0].Direction = System.Data.ParameterDirection.Input;
            parameters[1].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GET_RESOURCE_ROOM", parameters, "table1");
        }
        #endregion 

    }
}
